﻿using System;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Media.Animation;
using System.Windows.Controls;

namespace BaseControls
{
    /// <summary>
    /// This class is a animation for founded object (flash effect)
    /// </summary>
    public class ItemFound : Behavior<FrameworkElement>
    {
        //Default property values
        private Duration duration = TimeSpan.FromSeconds(1);

        private Storyboard myStoryboard;

        public ItemFound()
        {
            myStoryboard = new Storyboard();
        }
        public EventHandler Stopped;
        protected override void OnAttached()
        {
            base.OnAttached();
            Invoke();
        }
        public ClockState GetState()
        {
            return myStoryboard.GetCurrentState();
        }
        protected void Invoke()
        {
            if (myStoryboard.GetCurrentState() == ClockState.Stopped)
            {                                
                myStoryboard.Duration = duration;
                DoubleAnimationUsingKeyFrames OpacityAnim = new DoubleAnimationUsingKeyFrames();
                EasingDoubleKeyFrame KeyFrame01 = new EasingDoubleKeyFrame();
                KeyFrame01.KeyTime = new TimeSpan(0, 0, 0, 0, 50);
                KeyFrame01.Value = 0;
                EasingDoubleKeyFrame KeyFrame02 = new EasingDoubleKeyFrame();
                KeyFrame02.KeyTime = new TimeSpan(0, 0, 0, 0, 200);
                KeyFrame02.Value = 0;
                EasingDoubleKeyFrame KeyFrame03 = new EasingDoubleKeyFrame();
                KeyFrame03.KeyTime = new TimeSpan(0, 0, 0, 0, 250);
                KeyFrame03.Value = 1;
                EasingDoubleKeyFrame KeyFrame04 = new EasingDoubleKeyFrame();
                KeyFrame04.KeyTime = new TimeSpan(0, 0, 0, 0, 450);
                KeyFrame04.Value = 1;
                EasingDoubleKeyFrame KeyFrame05 = new EasingDoubleKeyFrame();
                KeyFrame05.KeyTime = new TimeSpan(0, 0, 0, 0, 500);
                KeyFrame05.Value = 0;
                EasingDoubleKeyFrame KeyFrame06 = new EasingDoubleKeyFrame();
                KeyFrame06.KeyTime = new TimeSpan(0, 0, 0, 0, 700);
                KeyFrame06.Value = 0;
                EasingDoubleKeyFrame KeyFrame07 = new EasingDoubleKeyFrame();
                KeyFrame07.KeyTime = new TimeSpan(0, 0, 0, 0, 750);
                KeyFrame07.Value = 1;
                EasingDoubleKeyFrame KeyFrame08 = new EasingDoubleKeyFrame();
                KeyFrame08.KeyTime = new TimeSpan(0, 0, 0, 0, 900);
                KeyFrame08.Value = 1;
                EasingDoubleKeyFrame KeyFrame09 = new EasingDoubleKeyFrame();
                KeyFrame09.KeyTime = new TimeSpan(0, 0, 0, 1, 250);
                KeyFrame09.Value = 0;

                OpacityAnim.KeyFrames.Add(KeyFrame01);
                OpacityAnim.KeyFrames.Add(KeyFrame02);
                OpacityAnim.KeyFrames.Add(KeyFrame03);
                OpacityAnim.KeyFrames.Add(KeyFrame04);
                OpacityAnim.KeyFrames.Add(KeyFrame05);
                OpacityAnim.KeyFrames.Add(KeyFrame06);
                OpacityAnim.KeyFrames.Add(KeyFrame07);
                OpacityAnim.KeyFrames.Add(KeyFrame08);
                OpacityAnim.KeyFrames.Add(KeyFrame09);

                myStoryboard.Duration = new TimeSpan(0, 0, 0, 1, 300);
                myStoryboard.Children.Add(OpacityAnim);
                Storyboard.SetTarget(OpacityAnim, AssociatedObject);
                Storyboard.SetTargetProperty(OpacityAnim, new PropertyPath("(Opacity)"));


                myStoryboard.Begin();
                myStoryboard.Completed += new EventHandler((obj, ev) =>
                {
                    if (Stopped != null)
                        Stopped(null, null);
                });
            }
        }
    }

}
